package com.leyou.controller;

import com.leyou.auth.utils.UserContext;
import com.leyou.dto.UserDTO;
import com.leyou.dto.UserDetails;
import com.leyou.entity.User;
import com.leyou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author jshstart
 * @creat 2020-12-22 16:48
 */
@RestController
@RequestMapping("/info")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> isUserExist(@PathVariable("data") String data,@PathVariable("type") Integer type){
       Boolean isExists= userService.isUserExists(data,type);
       return ResponseEntity.ok(isExists);
    }
    @PostMapping("/code")
    public ResponseEntity<Void> sendMessage(@RequestParam("phone") String phone){
        userService.sendMessage(phone);
        return ResponseEntity.ok().build();
    }
    @PostMapping
    public ResponseEntity<Void> registerUser(@Valid User user,
                                             @RequestParam("code") String code
                                             ){
        userService.registerUser(user,code);
        return ResponseEntity.ok().build();
    }
    @GetMapping
    public ResponseEntity<UserDTO> queryUserByNamePassword(@RequestParam("username") String username,
                                                           @RequestParam("password") String password)
    {
      UserDTO userDTO=  userService.queryUserByNamePassword(username,password);
        return ResponseEntity.ok(userDTO);
    }
    @GetMapping("/me")
    public ResponseEntity<UserDetails> getUser(){
        return ResponseEntity.ok(UserContext.getUser());
    }
}
